package com.justyh.ssm.job;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Random;

//import java.time.LocalDate;
//import java.time.format.DateTimeFormatter;

/**
 * 测试定时任务
 */
@Slf4j
@Component
public class WebTask {
    private static int maxJpgIndex=34;
    private static int maxGifIndex=32;
    private static String rootUtl="http://web.justcourse.top/img/";
    private static int index=1;
    //@Scheduled(cron = "0/5 * * * * ?")
   public void taskJob(){
     log.info("定时炸一次，啊哈哈哈...");
   }
   @Scheduled(cron = "0 0 19 * * MON-FRI")
   public void sendInfoJpgToDingDing(){
      /* int index1=new Random().nextInt(19) + 1;
       int index2=(index1+1)>maxJpgIndex?index1-1:index1+1;
       int index3=(index1+2)>maxJpgIndex?index2-2:index2+2;*/
       String image1=rootUtl+"daka"+String.valueOf(index)+".jpg";
       String image2=rootUtl+"daka"+String.valueOf(index)+".jpg";
       String image3=rootUtl+"daka"+String.valueOf(index)+".jpg";
       index=index==maxJpgIndex?1:index+1;
       String message="客官，喝碗毒鸡汤再打卡吧";
       sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=12f4a9bbdf1c071653e73d7f13937686e53ffcf1ae3a13792e25465db6b6d94b",image1,message);
       sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=9c0751c0c418cfc5a62173e5a6233596e3ecc384f4b77656ad126b09c3f1fb96",image2,message);
       sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=fc0503343b59e7a816acb6d4e7544337a82dc382d9a6e6b6d515acccd44ea251",image3,message);
   }
    @Scheduled(cron = "0 5 19 * * MON-FRI")
    public void sendInfoGifToDingDing(){
        int index1=new Random().nextInt(maxGifIndex) + 1;
        int index2=(index1+1)>maxGifIndex?index1-1:index1+1;
        int index3=(index1+2)>maxGifIndex?index1-2:index1+2;
        int index4=(index1+3)>maxGifIndex?index1-3:index1+3;
        String image1=rootUtl+"dakagif"+String.valueOf(index1)+".gif";
        String image2=rootUtl+"dakagif"+String.valueOf(index2)+".gif";
        String image3=rootUtl+"dakagif"+String.valueOf(index3)+".gif";
        String image4=rootUtl+"dakagif"+String.valueOf(index4)+".gif";
        String message="最后提醒打卡";
        sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=12f4a9bbdf1c071653e73d7f13937686e53ffcf1ae3a13792e25465db6b6d94b",image1,message);
        sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=9c0751c0c418cfc5a62173e5a6233596e3ecc384f4b77656ad126b09c3f1fb96",image2,message);
        sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=fc0503343b59e7a816acb6d4e7544337a82dc382d9a6e6b6d515acccd44ea251",image3,message);
        sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=bd650e38501716473f4bb9da00c00b0275b2083043805521a07867c96163ef4d",image4,message);
    }
    @Scheduled(cron = "0 0 18 * * MON-FRI")
    public void sendJpgIn18(){
        String image1=rootUtl+"daka"+String.valueOf(index)+".jpg";
        String message="客官，喝碗毒鸡汤再打卡吧";
        sendDingDingTalk("https://oapi.dingtalk.com/robot/send?access_token=bd650e38501716473f4bb9da00c00b0275b2083043805521a07867c96163ef4d",image1,message);
    }
   public void sendDingDingTalk(String url,String image,String message){
       HttpClient httpclient = HttpClients.createDefault();

       HttpPost httppost = new HttpPost(url);
       httppost.addHeader("Content-Type", "application/json; charset=utf-8");
//       String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"大家好，我是打卡先锋,现在是晚上7点整，打卡！打卡！打卡！重要的事情说三遍！。\n--来自瘦子AI\"}}";
     /*  if(Calendar.getInstance().get(Calendar.DAY_OF_WEEK)==6){
          image="http://web.justcourse.top/img/dakagif"+String.valueOf(new Random().nextInt(2) + 1)+".gif";
       }*/
       String textMsg="{\n" +
               "     \"msgtype\": \"markdown\",\n" +
               "     \"markdown\": {\n" +
               "         \"title\":\"打卡先锋\",\n" +
               "         \"text\": \"#### "+message+"\\n> 下班喽，及时打卡呦\\n> ![打卡]("+image+")\\n> ###### --来自瘦子AI \\n\"\n" +
               "     }\n" +
               " }";
       System.out.println(textMsg);
       StringEntity se = new StringEntity(textMsg, "utf-8");
       httppost.setEntity(se);

       try {
           HttpResponse response = httpclient.execute(httppost);
           if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
               String result= EntityUtils.toString(response.getEntity(), "utf-8");
               log.info(result);
           }
       }catch (Exception e){
           log.error("调用钉钉接口异常，原因：{}",e.getMessage());
       }
   }

    /*@Scheduled(cron = "0 0 18 4 10 *")
    public void sendDingTalk(){
        HttpClient httpclient = HttpClients.createDefault();

        HttpPost httppost = new HttpPost("https://oapi.dingtalk.com/robot/send?access_token=12f4a9bbdf1c071653e73d7f13937686e53ffcf1ae3a13792e25465db6b6d94b");
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");

        String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"祝大家中秋节快乐！\n--来自瘦子AI\"}}";
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httppost.setEntity(se);

        try {
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                String result= EntityUtils.toString(response.getEntity(), "utf-8");
                log.info(result);
            }
        }catch (Exception e){
            log.error("调用钉钉接口异常，原因：{}",e.getMessage());
        }

    }*/
   /* public static void main(String[] args){
        WebTask webTask=new WebTask();
        webTask.sendInfoToDingDing();
    }*/
}
